package com.guanyong.admin.controllers.api;

import com.guanyong.model.CommentModel;
import com.guanyong.services.CommentService;
import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * Created by mxk on 2017/6/4.
 */

@RestController
@RequestMapping("/api/comment/")
public class CommentApi {

    @Autowired
    CommentService commentService;

    @RequestMapping(value = "commentlist", method = RequestMethod.GET)
    ResultObject<HashMap<String,Object>> getcommentlist(HttpServletRequest request,
                                                         @RequestParam Integer classtype,
                                                         @RequestParam String objectid,
                                                         @RequestParam String commentid,
                                                         @RequestParam String tocommentid,
                                                         @RequestParam Integer parentid,
                                                         @RequestParam Integer reportct,
                                                         @RequestParam Integer show,
                                                         @RequestParam int pno,
                                                         @RequestParam int pagesize,
                                                         @RequestParam int allcount) {
        ResultObject<HashMap<String,Object>> result = new ResultObject<HashMap<String,Object>>();
        CommentModel commentModel =new CommentModel();
        commentModel.setfClasstype(classtype);
        commentModel.setfShow(show);
        commentModel.setfObjectid(objectid);
        commentModel.setfCommentid(commentid);
        commentModel.setfTocommentid(tocommentid);
        commentModel.setfParentid(parentid);
        commentModel.setfReportct(reportct);
        HashMap<String,Object> hashMap=commentService.GetCommentList(commentModel,
                pno,
                pagesize,
                allcount);
        result.setResult(hashMap);
        return result;
    }



    @RequestMapping(value = "updatecommentstate", method = RequestMethod.POST)
    ResultObject<Integer> updatecommentstate(HttpServletRequest request,
                                             @RequestParam Integer fid ,
                                             @RequestParam Integer state ,
                                             @RequestParam Integer parentid) {
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        Integer res=-1;
        //单个修改
        if (parentid>0){
            res=commentService.UpdateSate(fid,state);
        }
        else{
            //批量修改
            res=commentService.UpdatePraentIdSate(fid,state);
        }
        if (res >-1){
            resultObject.setCode(ResultCode.OperateOk);
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
        }
        return resultObject;
    }
}
